//
//  TJSelectCoinController.m
//  Teleport
//
//  Created by hc-101 on 2020/6/15.
//  Copyright © 2020 xtjun. All rights reserved.
//币子名字选择器

#import "TJSelectCoinController.h"

@interface TJSelectCoinController ()<UIGestureRecognizerDelegate, UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak, nonatomic) UIView *contentView;
@property (weak, nonatomic) UIView *maskView;
@property (weak, nonatomic) UIPickerView *pickerView;
@property (strong, nonatomic) NSArray *datas;

@property (copy, nonatomic) NSString *selectCoinName;

@end

@implementation TJSelectCoinController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.modalPresentationStyle = UIModalPresentationCustom;
    }
    return self;
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.5];
        self.contentView.tj_y = KScreenHeight - self.contentView.tj_height;
    } completion:^(BOOL finished) {
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addSubView];
}

- (void)addSubView {
    
    self.view.backgroundColor = [UIColor clearColor];
    
    UIView *maskView = [UIView tj_addToView:self.view];
    maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
    maskView.frame = self.view.bounds;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [maskView addGestureRecognizer:tap];
    tap.delegate = self;
    
    UIView *contentView = [UIView tj_addToView:maskView];
    contentView.backgroundColor = UIColor.whiteColor;
    contentView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, 150 + TabbarSafeBottomMargin);
    
    UIView *toolbar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 49)];
    toolbar.backgroundColor = UIColor_Hex(0xF1F1F1);
    [contentView addSubview:toolbar];
    
    UIButton *cancelButton = [UIButton buttonWithSuperView:toolbar font:UIFont_Px(15) color:UIColor_Hex(0x9EA0A4) title:@"取消".localizedString];
    cancelButton.tag = 1001;
    cancelButton.frame = CGRectMake(12, 0, [@"取消".localizedString getStringSizeWithWidth:200 font:UIFont_Px(15)].width + 16, 48);
    [cancelButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat determineButtonW = [@"确定".localizedString getStringSizeWithWidth:200 font:UIFont_Px(15)].width + 16;
    UIButton *determineButton = [UIButton buttonWithSuperView:toolbar font:UIFont_Px(15) color:UIColor_Hex(0x3781E7) title:@"确定".localizedString];
    determineButton.tag = 1002;
    determineButton.frame = CGRectMake(SCREEN_WIDTH - determineButtonW - 12, 0, determineButtonW, 49);
    [determineButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
 
    UIPickerView *pickerView = [[UIPickerView alloc] init];
    [contentView addSubview:pickerView];
    pickerView.backgroundColor = UIColor.whiteColor;
    pickerView.dataSource = self;
    pickerView.delegate = self;
    [pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(toolbar.mas_bottom);
        make.left.right.offset(0);
        make.bottom.offset(-TabbarSafeBottomMargin);
    }];
    
    _contentView = contentView;
    _maskView = maskView;
    _pickerView = pickerView;
    
    _datas = @[
        
        @{ @"name": @"USDT"},
         @{ @"name": @"BTC"}
        
    ];
    
    

    [self pickerView:pickerView didSelectRow:0 inComponent:0];

    
}

- (void)didClickButton:(UIButton *)button {
    
    [self dismissComplete:^{
        if (self.selectComplete && button.tag == 1002) {
            self.selectComplete(self.selectCoinName);
        }
    }];
    
    
}


- (void)didClickComplete {
    [UIView animateWithDuration:0.2f animations:^{
        self.contentView.tj_y = self.view.tj_height;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
}

#pragma mark - 返回多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

#pragma mark - 返回多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.datas.count;
    
}

#pragma mark - 返回内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

    return _datas[row][@"name"];
   
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
   

    self.selectCoinName = _datas[row][@"name"];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *pickerLabel = (UILabel *)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        [pickerLabel setFont:[UIFont systemFontOfSize:17]];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

- (void)dismiss {
    [self dismissComplete:nil];
}

- (void)dismissComplete:(void (^)(void))complete {
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        self.contentView.tj_y = KScreenHeight;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (complete) complete();
        }];
    }];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (touch.view != self.maskView) {
        return NO;
    }else {
        return YES;
    }
}






@end
